﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using Enterprise.Exceptions;

namespace Enterprise
{
    /// <summary>
    /// Initializes Enterprise.Cms with default values
    /// If no instance of this class is made, the setup is initialized.
    /// </summary>
    public class Settings : DataManager<Settings.Data>
    {
        public Settings():base(){}

        /// <summary>
        /// The startpage id for a domain
        /// </summary>
        public string StartPageId
        {
            get { return _data.StartPageId; }
            set { _data.StartPageId = value; }
        }

        /// <summary>
        /// The default title for all pages 
        /// </summary>
        public string Title
        {
            get { return _data.Title; }
            set { _data.Title = value; }
        }

        /// <summary>
        /// The footer text
        /// </summary>
        public string FooterText
        {
            get { return _data.FooterText; }
            set { _data.FooterText = value; }
        }

        public string HeaderTitle
        {
            get { return _data.HeaderTitle; }
            set { _data.HeaderTitle = value; }
        }

        public string Skin
        {
            get { return _data.Skin; }
            set { _data.Skin = value; }
        }

        protected override string GetID()
        {
            return Values.Settings;
        }

        private static Settings _instance;

        /// <summary>
        /// Singleton method to get the instance of Settings.
        /// If no instance is made, setup is initialized.
        /// </summary>
        /// <returns>Instance of Settings.</returns>
        public static Settings GetInstance()
        {
            if (_instance == null)
            {
                _instance = new Settings();
                if (_instance.Load())
                    _instance.LoadData();
                else             
                     _instance = Setup();
            }
            return _instance;
        }


        /// <summary>
        /// Setup creates database tables for modules.
        /// </summary>
        /// <returns>New instance of Settings class</returns>
        private static Settings Setup()
        {
            //Will be moved into separate files
            string SqlScript = @"IF EXISTS(SELECT name FROM sysobjects WHERE name = 'Objects' AND xtype='U') DROP TABLE [Objects];
                                CREATE TABLE [Objects]	
                                [id] [int] IDENTITY(1,1) NOT NULL,
	                            [ObjectData] [ntext] NULL,
	                            [PageID] [int] NULL,
	                            [RegionID] [varchar](50) NULL,
	                            [SortOrder] [int] NULL,
	                            [PortalID] [int] NULL";

            SqlConnection conn = new SqlConnection(Config.ConnectionString());
            conn.Open();
            SqlCommand cmd = new SqlCommand(SqlScript, conn);
            cmd.ExecuteNonQuery();
            conn.Close();

            Settings settings = new Settings();
            settings.Title = "Successfully installed settings.";
            settings.SaveData();
            return settings;
        }

        /// <summary>
        /// Dataclass for Settings.
        /// </summary>
        public class Data
        {
            public string StartPageId = string.Empty;
            public string Title = "Initializes Enterprise.CMS...";
            public string DefaultFooterText = "(&copy;) All rights reserved Company name ";
            public string FooterText = string.Empty;
            public string HeaderTitle = string.Empty;
            public string Skin = "enterprise.cms";
        }
    }
}
